﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.Model;
using WXYW.DAL;

namespace WXYW
{
    public partial class AccessoryTJ : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialDropDown();
            }
        }

        private void InitialDropDown()
        {
            AccessoryService aservice = new AccessoryService();
            IQueryable<AccessoryClass> accessoryClasses = aservice.GetAccessoryTypeList();
            typeDropDownList.Items.Add(new ListItem("所有类型", "0"));
            foreach (AccessoryClass accessoryClass in accessoryClasses)
            {
                typeDropDownList.Items.Add(new ListItem(accessoryClass.Name, accessoryClass.ID.ToString()));
            }

            ServiceStationService sservice = new ServiceStationService();
            IQueryable<ServiceStation> stations = sservice.GetALLServiceStations();
            servicestationDropDownList.Items.Add(new ListItem("所有平台", "0"));
            foreach (ServiceStation station in stations)
            {
                servicestationDropDownList.Items.Add(new ListItem(station.Name, station.ID.ToString()));
            }

            for (int i = 2010; i <= DateTime.Now.Year; i++)
            {
                yearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
                endYearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            yearDropDownList.SelectedIndex = 0;
            endYearDropDownList.SelectedIndex = endYearDropDownList.Items.Count - 1;

            for (int i = 1; i <= 12; i++)
            {
                monthDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
                endMonthDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            monthDropDownList.SelectedIndex = 0;
            endMonthDropDownList.SelectedIndex = DateTime.Now.Month - 1;
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            BindAccessory();
        }

        private void BindAccessory()
        {
            AccessoryService service = new AccessoryService();
            List<AccessoryTJModel> tjModels = service.GetAccessoryTJBy(modelNOTextBox.Text, Convert.ToInt32(typeDropDownList.SelectedValue), Convert.ToInt32(servicestationDropDownList.SelectedValue), Convert.ToInt32(yearDropDownList.SelectedValue), Convert.ToInt32(monthDropDownList.SelectedValue),
                Convert.ToInt32(endYearDropDownList.SelectedValue), Convert.ToInt32(endMonthDropDownList.SelectedValue));
            tjGridView.DataSource = tjModels;
            tjGridView.DataBind();
        }


        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            tjGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindAccessory();
        }

        protected void tjGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= tjGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (tjGridView.PageIndex + 1).ToString();
            }
        }
    }
}
